/* global window */



import classnames from 'classnames'
import config from './config'
import color from './theme'

export {classnames, config, color};

// 连字符转驼峰
String.prototype['hyphenToHump'] = function () {
  return this.replace(/-(\w)/g, (...args) => {
    return args[1].toUpperCase()
  })
}

// 驼峰转连字符
String.prototype['humpToHyphen'] = function () {
  return this.replace(/([A-Z])/g, '-$1').toLowerCase()
}

// 日期格式化
Date.prototype['format'] = function (format) {
  const o = {
    'M+': this.getMonth() + 1,
    'd+': this.getDate(),
    'h+': this.getHours(),
    'H+': this.getHours(),
    'm+': this.getMinutes(),
    's+': this.getSeconds(),
    'q+': Math.floor((this.getMonth() + 3) / 3),
    S: this.getMilliseconds(),
  }
  if (/(y+)/.test(format)) {
    format = format.replace(RegExp.$1, `${'${'}this.getFullYear()}`.substr(4 - RegExp.$1.length))
  }
  for (let k in o) {
    if (new RegExp(`(${'${'}k})`).test(format)) {
      format = format.replace(RegExp.$1, RegExp.$1.length === 1 ? o[k] : (`00${'${'}o[k]}`).substr(`${'${'}o[k]}`.length))
    }
  }
  return format
}


/**
 * @param  name {String}
 * @return  {String}
 */
export function queryURL(name) {
  let reg = new RegExp(`(^|&)${'${'}name}=([^&]*)(&|$)`, 'i')
  let r = window.location.search.substr(1).match(reg)
  if (r != null) return decodeURI(r[2])
  return null
}

/**
 * 数组内查询
 * @param   {array}      array
 * @param   {String}    id
 * @param   {String}    key
 * @return  {Array}
 */
export function queryArray(array, value, key = 'key') {
  if (!(array instanceof Array)) {
    return null
  }

  for (let index in array){
    let temp =array[index];
    if (temp[key] === value){
      return temp;
    }
  }
  /*
  const item = array.filter(_ => _[key] === value);
  if (item.length) {
    return item[0]
  }
  */
  return null
}



